package com.lottery.im.module.service.presenter;

import android.app.Activity;
import com.lottery.im.MyApplication;
import com.lottery.im.api.ApiClient;
import com.lottery.im.api.observer.BaseListObserver;
import com.lottery.im.api.observer.ShowTipListObserver;
import com.lottery.im.api.result.entity.integral.UserCashList;
import com.lottery.im.base.BaseFragmentPresenter;
import com.lottery.im.module.service.view.UserCashListView;
import com.lottery.im.util.ToastUtils;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
 * 作者：cyy
 * 时间：2018/9/21。
 */
public class UserCashListPresenter extends BaseFragmentPresenter<UserCashListView> {
  public UserCashListPresenter(Activity activity,
      UserCashListView iView,
      LifecycleProvider<FragmentEvent> provider) {
    super(activity, iView, provider);
  }

  String token = MyApplication.getInstance().getToken();
  String userId = MyApplication.getInstance().getUserInfo().getUserId();

  public void loadUserCashList(int startRow,int pageSize,int loadState){
    ApiClient.getApiRetrofitInstance()
        .queryUserCashList(userId,token,startRow,pageSize)
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(provider.bindToLifecycle())
        .subscribe(new BaseListObserver<UserCashList>() {
          @Override public void onStart() {
            if(loadState==0){
              showLoadingProgress("正在加载");
            }
          }

          @Override public void onSuccess(List<UserCashList> t) {
            iView.loadUserCashListSuccess(t,loadState);
            closeLoadingProgress();
          }

          @Override public void onFail(String code, String msg) {
            ToastUtils.showToast(activity,msg);
            closeLoadingProgress();
          }

          @Override public void onException(String msg) {
            //ToastUtils.showToast(activity,msg);
            closeLoadingProgress();
          }

          @Override public void onFinish() {
            closeLoadingProgress();
          }
        });
  }

  public void loadUserCashListFilter(String startDate,String endDate,
      String accType,int startRow,int pageSize,int loadState){
      ApiClient.getApiRetrofitInstance()
        .queryUserCashListFilter(userId,token,startDate,endDate,accType,startRow,pageSize)
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(provider.bindToLifecycle())
        .subscribe(new ShowTipListObserver<UserCashList>(activity) {
          @Override public void onSuccess(List<UserCashList> t) {
            iView.loadUserCashListFilterSuccess(t);
          }
        });
  }
}
